import { createRouter, createWebHistory } from "vue-router";
// 登录组件公用
import Login from "../views/Login.vue";
// 前台组件
import Home from "../views/home/Home.vue";
// 后台组件
import Default from "../views/admin/layout.vue";
import User from "../views/admin/user.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/", redirect: "/home" },
    { path: "/home", component: Home },
    {
      path: "/admin",
      component: Default,
      children: [{ path: "user", component: User }],
    },
  ],
});

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将访问的路径
  // from 表示从哪个路径跳转而来
  // next 是一个函数，表示放行
  // if (to.path === "/" || to.path === "/home") {
  //   return next();
  // } else {
  //   // 获取 token
  //   const tokenStr = window.sessionStorage.getItem("token");
  //   if (!tokenStr) return next();
  // }
  next();
});

export default router;
